var user_detail;
var checkLength=/([\u4E00-\u9FA5\uf900-\ufa2d])/g;
var  randomNum = getRandomNum();
//匹配中文 数字 字母 下划线
var hasIllegalChar = function (str) {
    var pattern = /^[\w\u4e00-\u9fa5]+$/gi;
    if(pattern.test(str)){
        return false;
    }
    return true;
}
  //初始化Form1
function doOnFormInit1(){
	if (typeof(editForm1) != "object")  return;
	
	editForm1.attachEvent("onButtonClick", function(name, command) {
		switch(command) {
			case 'save':doSave();break;
			case 'cancle':doCancle();break;
		}
	});
	
//	var status_combo=editForm1.getCombo('status');
//	status_combo.addOption(parent.statusComboData);
//	status_combo.deleteOption("");
//	status_combo.setComboValue('');
//	status_combo.readonly(true);
	
	$("input[name='roleName']").focus(function () {
		$("input[name='nameTag']").css({color:"#666666"});
		editForm1.setItemValue("nameTag","20个字符以内");
	});
	$("input[name='roleName']").blur(function () {
		var name=$.trim($(this).val());
		editForm1.setItemValue("nameTag","");
		$("input[name='nameTag']").css({color:"red"});
		if(name.length==0){
			editForm1.setItemValue("nameTag","请填角色名");
		}else if(name.replace(checkLength,'**').length>15){
			editForm1.setItemValue("nameTag","20个字符以内");
    	}
	});
	editForm1.disableItem("nameTag");
	$(editForm1.getInput("sortNum")).attr("maxlength",9);
    if(type=='add'){
        checkParam(randomNum);
    }
	if(type=='edit')
		doOnForm1EditInit();
}

//判断是否是数字
function isDigit(str) {
	var reg = new RegExp("^[1-9]*[1-9][0-9]*$");
	if (str == null || str == '') return true;
	if (reg.test(str)) return true;
	return false;
}

//完善编辑用户详情
function doOnForm1EditInit(){
    if (!parent.editRoleInfo||!parent.editRoleInfo.id) return;

    var id= $.trim(parent.editRoleInfo.id);
    if(!id){
        $func.alert("所选数据有问题,请刷新页面!",	"id");
        return;
    }
    //验证
    checkParam(id);
	editForm1.setItemValue("roleName",escape2Html(parent.editRoleInfo.role_name));
	editForm1.setItemValue("remark",escape2Html(parent.editRoleInfo.remark));
	editForm1.setItemValue("sortNum",escape2Html(parent.editRoleInfo.sort_num));
	//editForm1.setItemValue("status",parent.editRoleInfo.status);

}

//校验新增/编辑form表单
function checkForm(){
	$("input[name$='tag']").val("");
	var name=$.trim(editForm1.getItemValue("roleName"));
	var remark=$.trim(editForm1.getItemValue("remark"));
//	var status=$.trim(editForm1.getItemValue("status"));

	if(!name){
		$func.alert('请填角色名！', 'red');
		return false;
	}
//	if(status.length==0){
//		$func.alert('请选择状态！', 'red');
//		return false;
//	}
    if( hasIllegalChar(name) ){
        $func.alert('角色名含有特殊字符！', 'red');
        return false;
    }
	if(name.replace(checkLength,'**').length>20){
		$func.alert('角色名超出20个字符 ', 'red');
		return false;
	}
	if(remark.replace(checkLength,'**').length>100){
		$func.alert('角色描述100个字符', 'red');
		return false;
	}
	var sortNum = editForm1.getItemValue("sortNum");
	if (sortNum != null && sortNum != '' && !isDigit(sortNum)) {
		$func.alert("【排序】格式有误，须为正整数，请修改后再保存","red");
		return false;
	}
	return true;
}
//保存动作
function doSave(){
	if(!checkForm())return;
	var formData = editForm1.getFormData();
    var postData={roleName:formData.roleName};

    postData['remark']=formData.remark;
    postData['sortNum']=formData.sortNum;
//	if(formData.status) postData+="&status="+formData.status;
	if(type=='edit'&&parent.editRoleInfo&&parent.editRoleInfo.id){
         postData['id']=parent.editRoleInfo.id;
    }
    if(type=='add' ){
        postData['randomNum']=randomNum;
    }
    console.log(postData);
	$.ajax({
        url: basePath+'manageRole/save',
        type: 'post',
        dataType: 'json',
        data:postData,
        success: function (back) {
        	if(!back||!back.result){
        		$func.alert(back.message?back.message:"保存失败", "red");
        		return;
        	}
        	$func.alert("保存成功！", "green");
        	parent.loadData();
        	doCancle();
        }
    });
}
//取消动作
function doCancle(){
	parent.editRoleWin.close();
}
